<?php
require_once('kernel.inc');
$CATALOGUE = new kernel($moduleid);
$CATALOGUE->SetParams("language=".$lang.';user='.$AUTH->user['id']);

$tree = $MECCANO->GetFlatList($moduleid);
$SMARTY->assign('tree', $tree);

$tableInfo = $CATALOGUE->GetTable();
$SMARTY->assign('tableInfo', $tableInfo);

$params       = 'clear';
$error        = false;
$result       = false;
$previw       = false;
$photo        = false;

if($AUTH->logedstatus === true && ($AUTH->permission == 'system' || isset($AUTH->permission[$moduleid])))
{
   if(isset($_REQUEST['dele']))
   {
      $CATALOGUE->Delete($_REQUEST['dele']);
      SmartyPaginate::reset($_SYS['meccano']['pid']);
      SmartyPaginate::setLimit($MECCANO->system['items'], $_SYS['meccano']['pid']);
      SmartyPaginate::setPageLimit($MECCANO->system['pages'], $_SYS['meccano']['pid']);
   }

   switch ($event)
   {
      case 13:
      /**
      * Edit Entry
      */
      if($id !== false)
      {
         $vars = $CATALOGUE->View($id);
         $SMARTY->assign('vars', $vars);
      }

      case 11:
      /**
      * Save Edited or Newly added Entry
      */
         if($submit == true)
         {
            $t = 0;
            foreach($tableInfo['fields'] AS $field)
            {
               if($field['required'] == 0 || ($field['required'] == 1 && isset($_REQUEST[$field['name']]) && $_REQUEST[$field['name']] != false))
               {
                  $t_column[$t] = $field['name'];
                  $c_type[$t]   = $field['type'];
                  if($field['form'] == 'data')
                  {
                     $day   = isset($_REQUEST[$field['name']]['Date_Day'])   ? $_REQUEST[$field['name']]['Date_Day']   : '';
                     $month = isset($_REQUEST[$field['name']]['Date_Month']) ? $_REQUEST[$field['name']]['Date_Month'] : '';
                     $year  = isset($_REQUEST[$field['name']]['Date_Year'])  ? $_REQUEST[$field['name']]['Date_Year']  : '';
                     if(checkdate($month, $day, $year))
                     {
                        $c_value[$t] = $year.'-'.$month.'-'.$day.' '.date("H:i:s");
                     } else
                     {
                        $c_value[$t] = date(time() + 1209600, "Y-m-d H:i:s");
                     }
                  } else
                  {
                     $c_value[$t]  = isset($_REQUEST[$field['name']]) ? $_REQUEST[$field['name']] : '';
                  }
                  $t++;
               } elseif($field['name'] == 'cat' || $field['name'] == 'lang')
               {
                  $t_column[$t] = $field['name'];
                  $c_type[$t]   = $field['type'];
                  $c_value[$t]  = ${$field['name']};
                  $t++;
               } else
               {
                  $error[] = $field['name'];
               }
            }
            
            if($error === false)
            {
               $sql = '';
               if($id != false)
               {
                  $sql = 'UPDATE ' . $module['table'] . ' SET ';
                  for($i=0; $i<$t; $i++)
                  {
                     if($i != 0) $sql .= ',';
                     $sql .= ' ' . $t_column[$i] . ' = "%' . $c_type[$i] . '"';
                  }
                  $sql .= ' WHERE id = ' . $id;
                  $DB->DB_QueryBuilder($sql, $c_value);
               } else
               {
                  $sql  = ' INSERT INTO ' . $module['table'] . ' (';
                  $sql .= implode(', ', $t_column) . ') VALUES ("%';
                  $sql .= implode('", "%', $c_type) . '")';
                  $DB->DB_QueryBuilder($sql, $c_value);
               }
               $DB->DB_Query();
               $make = true;
            }
         }

      $SMARTY->display('admin.editcatalogue.tpl.html');
      break;

      default:
      $vars =  $CATALOGUE->Get($category);
      if($vars)
      {
         SmartyPaginate::setTotal(count($vars), $_SYS['meccano']['pid']);
         SmartyPaginate::setUrl($_SERVER['PHP_SELF'], $_SYS['meccano']['pid']);
         $SMARTY->assign('vars', array_slice($vars, SmartyPaginate::getCurrentIndex($_SYS['meccano']['pid']), SmartyPaginate::getLimit($_SYS['meccano']['pid'])));
         SmartyPaginate::assign($SMARTY, 'paginate', $_SYS['meccano']['pid']);
      }

      $SMARTY->display('admin.catalogue.tpl.html');
   }
} else
{
   echo $AUTH->frame_redirector();
}